package lyq.zkhw;

import com.alibaba.druid.pool.DruidDataSource;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ZKHWClient {


    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    static ArrayList<String> infos = new ArrayList<String>();
    static ZkClient zkClient ;

    private void connetZK(){
        // 创建zkclient
         zkClient = new ZkClient("linux121:2181,linux122:2181,linux123:2181");
        List<String> childrens = zkClient.getChildren("/mysqlConfs");
        //第一次获取服务器信息,所有的子节点
        for (String children : childrens) {
            Object o = zkClient.readData("/mysqlConfs/" + children);
            infos.add(String.valueOf(o));
        }
    }

    public void keeper(){
        List<String> childrens = zkClient.getChildren("/mysqlConfs");
        String path = childrens.get(0);
       zkClient.subscribeDataChanges("/mysqlConfs/" + path, new IZkDataListener() {
                        @Override
                        public void handleDataChange(String dataPath, Object data) throws Exception {
                            release();
                            System.out.println("Zookeeper配置信息有变化，释放资源");
                            String[] split = String.valueOf(data).split(",");
                            System.out.println(Arrays.stream(split).toArray().toString());
                            loadMysq1Info(split);
                            exeSQl(3);
                        }

                        @Override
                        public void handleDataDeleted(String dataPath) throws Exception {
                            release();
                        }
                    });
    }

    ResultSet resultSet ;
    PreparedStatement ps ;
    Connection connection ;

    // 连接数据库
    public void loadMysq1Info(String[] s) throws Exception {
//        String ipPort = infos.get(0);
//        String[] s = ipPort.split(",");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(s[0]);
        dataSource.setPassword(s[1]);
        dataSource.setDriverClassName(s[2]);
        dataSource.setUrl(s[3]);
        dataSource.setInitialSize(5);
        // 3.根据连接池获取连接
         connection = dataSource.getConnection();
    }
    //执行sql
    public void exeSQl(int param) throws SQLException {
        // 4.创建预编译sql语句对象
        String sql = "select * from user where id = ?";
        ps = connection.prepareStatement(sql);

        // 5.设置参数
        ps.setInt(1,param);

        // 6.执行sql语句,处理结果
        resultSet = ps.executeQuery();
        User user = null;
        while (resultSet.next()) {
            user = new User();
            user.setId(resultSet.getInt("id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
        }
        System.out.println(user);

    }
    //释放资源
    public void release(){
        // 7.释放资源
        JDBCUtils.release(resultSet,ps ,connection );
    }





    public static void main(String[] args) throws Exception {

        ZKHWClient zkhwClient = new ZKHWClient();
        zkhwClient.connetZK();
        zkhwClient.loadMysq1Info(infos.get(0).split(","));
        zkhwClient.exeSQl(1);
        zkhwClient.release();
        zkhwClient.keeper();
        Thread.sleep(2000);
        Thread.sleep(Integer.MAX_VALUE);
    }

}
